"""
@-*- coding: utf-8 -*-
@ python: 3.12.3
@ 创建者: JacksonCode
@ 创建时间: 2025-10-13
"""
import base64

from Crypto.PublicKey import RSA
from Crypto.Cipher import PKCS1_OAEP


def rsa_generate():
    item = {}
    rsa_key = RSA.generate(1024)
    pbk = rsa_key.publickey().exportKey()
    item["public"] = pbk
    # print(pbk)
    private_key = rsa_key.export_key()
    item['private'] = private_key
    # print(private_key)
    return item


public = '-----BEGIN PUBLIC KEY-----\nMIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCpp8A8/67GMcXQuOwUEbZySxQZK8YCSY4/GA5JBbuknRahCmAW07GEifxkOLWEo8NGwGxafSmaQwt6QWIhou2cEC9/VHd8Au8bn88keBeJlukRiEBuYwdOirJsS3S9m1XMPZOed4u/9zo4VQwnjqqPnbnNMxWxBwJTNr1rPdSkFQIDAQAB\n-----END PUBLIC KEY-----'

private = '-----BEGIN RSA PRIVATE KEY-----\nMIICdgIBADANBgkqhkiG9w0BAQEFAASCAmAwggJcAgEAAoGBAKmnwDz/rsYxxdC47BQRtnJLFBkrxgJJjj8YDkkFu6SdFqEKYBbTsYSJ/GQ4tYSjw0bAbFp9KZpDC3pBYiGi7ZwQL39Ud3wC7xufzyR4F4mW6RGIQG5jB06KsmxLdL2bVcw9k553i7/3OjhVDCeOqo+duc0zFbEHAlM2vWs91KQVAgMBAAECgYAK+okNKXqU5smnTSAx2EeXM9aUHmoM4derq5HZFd7jsyqwtywqrf5R1mAX2Dee4r4TSQZ6AJl9mxMWET8ZcCKyGHT+Vjuk5ivCZ4R1EF0vgdFoz3Zmmr6M+f7TYSBf42NuhDYYmnUaISt/lXjS3HvJZt3GZ2teu6VuawTe32XegQJBAPDEkWlQx/7kFjxrh+3eIuGtKL26vcIpZTs8l6qT6FNuaRIE3S5/Y2We7v6KcSPcwgrQgPShXyCE26kk7QR5U3UCQQC0Y3Tv68fi7J+o4BsC73bTHmoJSYD+PT3YngKB3dBn4IMAyJgVk4ogY0jL42ub1wul6j9QhD8gXlDVSiX0+xohAkAi609vp9CpzijPcIURYbNLadLL2aal310YP0x75+G9H/lLLLtrf/xJg8iD0E5idiUKbn0VfcYPMFWvbXSWvoX1AkBQtp/AKnPJ6a6W1Y/AhH1agaVYencpSyW9IxZkHWf+0spLM3Y5UUbgrwwHjr67gEFJ/8JMuJ+2T/1DMmZBc4mBAkEA3HAy+GtSU2zz1oDnUN6FkVadgDOhxV3zU/wu45Ss3x3OiWZTimwLR+zFw0nAlDSTfXr0hssrZLKN+I0BfrPgYg==\n-----END RSA PRIVATE KEY-----'


def rsa_encrypt(pbk, data):
    rsa_key = RSA.importKey(pbk)
    cipher = PKCS1_OAEP.new(rsa_key)
    encode_text = cipher.encrypt(data.encode())
    res = base64.b64encode(encode_text)
    # print(res.decode())
    return res.decode()


# encode_text = "EAgix7WJaOxmMX6f+ywiZ7xMecrSQtQo1TQX7Dk2HU++ieI24XGiyQdOETLQh36Cr6DrE+mG0+nSoSdJOdTZkohfKHtn9PKSryiKBiNNTjDlmgVdtNgE12VmtoSRMaWc8LjPU3sHmQr4Q+BYkMd98gSc67M5YA4/fg4XmgyzCs+4EimApT8WNdJ+zideTkbcokGK5G3dDV6LSyh7JCVRoWX2AytwmzJC1Q7yKKqt2q22S8nNrDFn0Gm8blo91wMDKLdL/A23Qu3Syknbteq85+zyKjAcVF7HTnzOlewQ+4tdL5gIyJASr5h9phMV/+e+r6drwSoEAyhTnH1aFbbFbw=="


def rsa_decrypt(private, encode_text):
    cipher_decode = PKCS1_OAEP.new(RSA.importKey(private))
    decode_msg = cipher_decode.decrypt(base64.b64decode(encode_text))
    # print(decode_msg.decode())
    return decode_msg.decode()


if __name__ == '__main__':
    obj = rsa_generate()
    print(obj)

    ret = rsa_encrypt(public, "123456")
    # ret = rsa_encrypt(public, "080123456789")
    print(ret)

    item = "I4Oof6nluxm4R2S86lPr+vy4IOUR68ItBY/TYEJEcAOlEVDwNnc2GvLoW2eYNFJlcl8zjKXKCYZanpgVx39BNKT5kvK0y+SeYq0jR8rza67UpESz8JlaapP8emk46kTW77JqDXQdIGs4ivUjaIJrJMO9201ggRydsP6ovsqwHl8="

    result = rsa_decrypt(private, item)
    print(result)

    # encode_text = "gvUQl28qTtUBezQCBMu0ya/TIea6zUt7N0k9SAQFkgQcGG6SC1F6UexQCLemwhNaIe6nl51hGB4c/pPUx9XzotOej4Vc3+QygGqfT4mtdGFcRvO3QFD6nQ/e/6dBFR1TZRQ29zxOkrVNfXYsxGqJeT39BvOGKEvxDne+LRn/rUW5MGZ+qG82rFdOEQnmc15PSv0XDd/405TmK/j/DcUoHrggc/aVBCg+iTQ2MbFfQUiUyaefev/gMHKQy3iaK9YWD0+Xo0hnaDkbBzBnu5Wa4PnDy7eXKkTfQWTwyCwJPxEa80qz2t36TMLlCXThUVVksgXJnA2u6eRNWO5WP/s21w=="
    # res = rsa_decrypt(obj.get("private"), ret)
    res = rsa_decrypt(private, ret)
    print(res)
